package com.applancer.parser;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.applancer.dataprovider.DataType;
import com.applancer.model.GeneralModel;

public class GeneralParser {

	protected GeneralParser() {

	}

	public static GeneralParser createInstance(DataType.Type type) {
		switch (type) {
		case MenuData:
			return new MenuParser();
		case Comment:
			return new CommentParser();
		case Config:
			return new ConfigParser();
		case Post:
			return new FeedParser();
		case User:
			return new UserParser();
		case Home:
			return new HomePaser();
		default:
			return new GeneralParser();
		}

	}

	public List<GeneralModel> parseData(String json) {
		if (json.equals(""))
			return null;

		List<GeneralModel> ldata = new ArrayList<>();
		JSONArray arrData;

		try {
			JSONObject object = new JSONObject(json);
			arrData = object.getJSONArray("results");

			if (arrData.length() == 0)
				return ldata;

		} catch (Exception e) {

			Log.e(this.getClass().toString(), e.toString());
			return null;
		}

		parseArray(ldata, arrData);
		return ldata;
	}

	protected List<GeneralModel> parseData(JSONObject object) {
		return null;
	}

	protected List<GeneralModel> parseData(JSONArray arrData) {
		List<GeneralModel> ldata = new ArrayList<>();

		parseArray(ldata, arrData);
		return ldata;
	}

	protected void parseArray(List<GeneralModel> ldata, JSONArray arr) {
		for (int i = 0; i < arr.length(); i++) {

			JSONObject obj;
			try {
				obj = arr.getJSONObject(i);
			} catch (Exception e) {
				continue;
			}
			GeneralModel model = (GeneralModel) parseModel(obj);
			if (model != null)
				ldata.add(model);
		}
	}

	public GeneralModel parseModel(String str) {
		return null;
	}

	public GeneralModel parseModel(JSONObject obj) {
		return null;
	}

	public List<GeneralModel> parseModelArray(JSONObject obj) {
		return null;
	}
}
